<template>
  <div class="default-main ba-table-box">
    <div class="block">
      <div class="table-block">
        <el-alert class="ba-table-alert"
                  v-if="baTable.table.remark"
                  :title="baTable.table.remark"
                  type="info"
                  show-icon />
        <!-- 表格顶部菜单 -->
        <TableHeader :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
                     :quick-search-placeholder="t('Quick search placeholder', { fields: '订单号' })">
          <template #advertiser_id>
            <el-select v-model="baTable.comSearch.form['advertiser_id']"
                       placeholder="选择广告账户(可模糊搜索)"
                       filterable
                       multiple
                       clearable
                       collapse-tags
                       collapse-tags-tooltip
                       style="width: 100%;">
              <el-option v-for="item in adAcounts"
                         :key="item.id"
                         :label="item.advertiser_id"
                         :value="item.id">
                <span style="float: left">{{ item.advertiser_id }}</span>
                <span style=" float: right;color: #909399;font-size: 12px; ">{{ item.advertiser_name
 }}</span>
              </el-option>
            </el-select>
          </template>
          <template #month>
            <el-date-picker v-model="baTable.comSearch.form['month']"
                            type="month"
                            placeholder="选择月份"
                            value-format="YYYY-MM" />
          </template>
        </TableHeader>

        <el-row style="margin: 10px 0;"
                :gutter="10">
          <el-col :span="1.5">
            <el-button type="warning"
                       @click="exportExcelOperate"
                       :loading="state.buttonLoading"
                       v-if="baTable.auth('export')">
              <Icon :color="'#ffffff'"
                    name="el-icon-Position" />导出
            </el-button>
          </el-col>
        </el-row>
        <Table></Table>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, provide, onBeforeMount } from 'vue'
import { ElMessageBox } from 'element-plus'
import baTableClass from '/@/utils/baTable'
// import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi, fileDownload } from '/@/api/common'
import { useAdminInfo } from '/@/stores/adminInfo'
import Cookies from 'js-cookie'
import { useI18n } from 'vue-i18n'
import { concat } from 'lodash-es'
import { exportExcel } from '/@/api/backend/advertisement/advaccount'
import { getAdvaccountList } from '/@/api/backend/common'
import { routePush } from '/@/utils/router'

defineOptions({
    name: 'advertisement/advaccount',
})
const { t } = useI18n()
const state: {
    buttonLoading: boolean
} = reactive({
    buttonLoading: false
})

const adminInfo = useAdminInfo()
const adAcounts = ref([] as any[])
let optButtons: OptButton[] = [
    {
        render: 'tipButton',
        name: '每日明细',
        title: '',
        text:  '每日明细',
        type: 'primary',
        icon: '',
        class: 'newButton',
        disabledTip: false,
        click: (row: TableRow) => {
            routePush({
                path: '/admin/advertisement/advertising',
                query: {
                    advertiser_id: row.advertiser_id
                }
            })
        }
    }
]
optButtons = concat(defaultOptButtons(['edit', 'delete']), optButtons) 
optButtons[1].display = (row) => {
    return row.id != adminInfo.id
}

const baTable = new baTableClass(
    new baTableApi('/advertisement/advaccount/'),
    {   
        column: [
            {
                label: '广告账户', 
                prop: 'advertiser_id', 
                align: 'center', 
                comSearchRender:'slot',
                comSearchSlotName: 'advertiser_id',
                show: false
            },
            {
                label: '月份',
                prop:  'month',
                operator: '=', 
                comSearchRender:'slot',
                comSearchSlotName: 'month',
                show: false
            },
            {   label: '广告账户', 
                prop: 'advertiser_id', 
                align: 'center',
                operator: false
            },
            {   label: '广告名称', 
                prop: 'advertiser_name', 
                align: 'center',
                operator: false
            },
            {   label: '花费', 
                prop: 'spend', 
                align: 'center',
                operator: false
            },
            {   label: '统计月份', 
                prop: 'month', 
                align: 'center',
                operator: false
            },
            {
                label: t('Operate'),
                align: 'center',
                width: '100',
                render: 'buttons',
                buttons: optButtons,
                operator: false
            }
        ],
        dblClickNotEditColumn: ['all']
    },
    {
        defaultItems: {
            status: '1',
        }
    }
)

provide('baTable', baTable)

baTable.mount()
baTable.getIndex()

const exportExcelOperate = () => {
    state.buttonLoading = true
    baTable.table.loading = true
    ElMessageBox.confirm(
        '是否确认导出所有数据项?',
        '警告',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        type keymaps = {
            [key: string]: string | number
        }
        const obj:keymaps = {}
        baTable.table.filter?.search!.forEach(v => {
            obj[v.field] = v.val
        }) || []
        exportExcel('post', obj).then(response => {
            fileDownload(response, Cookies.get('filename') || 'file.xlsx')
        }).catch(err => {
            state.buttonLoading = false
            baTable.table.loading = false
        }).finally(() => { 
            state.buttonLoading = false
            baTable.table.loading = false
        })
        state.buttonLoading = false
        baTable.table.loading = false
    }).catch(() => {
        state.buttonLoading = false
        baTable.table.loading = false
    })
}

const getAdAcounts = () => {
    getAdvaccountList().then(res => {
        if(res.data && res.data.length) {
            adAcounts.value = res.data
        }
    })
}

onBeforeMount(() => {
    getAdAcounts()
})
</script>

<style scoped lang="scss">
:deep(.el-date-editor.el-input){
    width: var(--el-input-width);
}

</style>
